package com.diyunapp.xilinapp.me;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GiveCoinActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.friend_name_edt)
    EditText friendNameEdt;
    @Bind(R.id.give_num_edt)
    EditText giveNumEdt;
    @Bind(R.id.sure_to_give)
    TextView sureToRecharge;
    private View headView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_give_coin);
        ButterKnife.bind(this);
        headView = findViewById(R.id.gico_menubar_inc);
        super.MenuWidget(headView);
    }
    @OnClick({R.id.go_back,R.id.sure_to_give})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
               finish();
                break;

            case R.id.sure_to_give:
                String name=friendNameEdt.getText().toString().trim();
                String num=giveNumEdt.getText().toString().trim();
                if (name.equals("")){
                    Toast.makeText(this,"要转赠的好友为空",Toast.LENGTH_SHORT).show();
                }else if(num.equals("")){
                    Toast.makeText(this,"未填写转赠数量",Toast.LENGTH_SHORT).show();
                }
                else {
                            //转赠
                        giveCoin();
            }


        }
    }
    private void giveCoin() {
        String name=friendNameEdt.getText().toString().trim();
        String num=giveNumEdt.getText().toString().trim();
        String url = ""+ MainActivity.url+"act=goods&op=to_turn&username="+MainActivity.myLogin.getUsername()+"&user_name="+name+"&give_num="+num+"";

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                toast("转赠成功");
                            } else {
                                toast("转赠失败");
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }
}
